package com.gk.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.chat.common.TokenContext;
import com.gk.chat.dto.FriendDTO;
import com.gk.chat.mapper.ContactMapper;
import com.gk.chat.mapper.UsersMapper;
import com.gk.chat.pojo.Contact;
import com.gk.chat.service.ContactService;
import com.gk.chat.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements ContactService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private TokenContext tokenContext;
    @Autowired
    private ContactMapper contactMapper;



    /**
     * 获取好友列表
     * @return
     */
    @Override
    public List<FriendDTO> getContactList() {

        //获取登录用户的id
        Long loginUserId= Long.valueOf(redisUtil.getLoginUserId(tokenContext.getToken()));

        LambdaQueryWrapper<Contact> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Contact::getUserId,loginUserId)
                .eq(Contact::getStatus,1)
                .select(
                        Contact::getRemark,
                        Contact::getFriendId,
                        Contact::getUserId,
                        Contact::getGroupId
                );
        return contactMapper.selectFriends(wrapper);
    }

    /**
     * 搜索用户信息服务接口
     * @return
     */
    @Override
    public List<FriendDTO> ServeContactGroupList() {
        //获取登录用户的id
        Long loginUserId= Long.valueOf(redisUtil.getLoginUserId(tokenContext.getToken()));
        int count=contactMapper.selectFriendsCount(loginUserId);
        FriendDTO friendDTO=new FriendDTO();
        friendDTO.setCount(count);
        friendDTO.setId(0);
        friendDTO.setName("全部");
        friendDTO.setSort(0);
        List<FriendDTO> contactGroupList=new ArrayList<>();
        contactGroupList.add(friendDTO);
        return contactGroupList;
    }
}
